<?php
/**
 * Created by IntelliJ IDEA.
 * User: 张伯发
 * Date: 2019/8/5
 * Time: 17:54
 */

namespace App\Common;


use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use phpDocumentor\Reflection\Types\This;
use Swoft\Bean\Annotation\Mapping\Bean;

/**
 * Class Elastic
 * @Bean()
 * @author 张伯发 2019/8/5 17:54
 */
class Elastic
{

    /**
     * @var Client
     */
    private $client;

    /**
     * @var array|null
     */
    private $hosts;

    /**
     * 重试次数
     * @var int
     */
    private $retryCount = 2;

    public function __construct()
    {
        $this->hosts = [
            '192.168.99.100:9200',         // IP + Port
//            '192.168.1.2',              // Just IP
//            'mydomain.server.com:9201', // Domain + Port
//            'mydomain2.server.com',     // Just Domain
//            'https://localhost',        // SSL to localhost
//            'https://192.168.1.3:9200'  // SSL to IP + Port
        ];

        var_dump('hello');
    }

    public function get()
    {
        $this->client = ClientBuilder::create()
            ->setHosts($this->hosts)
            ->setRetries($this->retryCount)// 重连次数，这个值应该是用集群的数量
            ->setConnectionPool('\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool', [])
            ->build();
    }
}
